package com.test.list;

import java.util.Arrays;

public class ArrayList implements  List{

    private Object[] elementDate;
    private int size;
    public ArrayList(int size){
        elementDate=new Object[size];
    }
    public ArrayList(){
        this(4);
    }



    @Override
    public int size() {
        return size;
    }

    @Override
    public Object get(int i) {
        if(i<0 || i>=size)
            throw new MyoutArrayException("输入的索引超界");
        return elementDate[i];

    }

    @Override
    public boolean isEmpty() {
        if(size==0){
            return true;
        }
        return false;
    }

    @Override
    public boolean contains(Object e) {
        return false;
    }

    @Override
    public int indexOf(Object e) {
        return 0;
    }

    @Override
    public void add(int i, Object e) {
      if(i<0 || i>elementDate.length)
          throw new MyoutArrayException("插入超界");
      if(size==elementDate.length)
      grow();
      for(int j=size;j>i;j--){
          elementDate[j]=elementDate[j-1];
      }
      elementDate[i]=e;
      size++;
    }

    @Override
    public void add(Object e) {
        add(size,e);
        //grow();
  //      if(size>=elementDate.length) {
//            Object[] newArray = new Object[2 * size];
//            for (int i = 0; i < elementDate.length; i++) {
//                newArray[i] = elementDate[i];
//            }
//            elementDate = newArray;
//
//        }
//        if(size>=elementDate.length)
//        elementDate=Arrays.copyOf(elementDate,elementDate.length*2);
       // System.out.println(elementDate.length);
//     elementDate[size]=e;
//        size++;
    }
    public void grow(){
        if(size>=elementDate.length)
            elementDate=Arrays.copyOf(elementDate,elementDate.length*2);
    }
    @Override
    public boolean addBefore(Object obj, Object e) {
        return false;
    }

    @Override
    public boolean addAfter(Object obj, Object e) {
        return false;
    }

    @Override
    public Object remove(int i) {
        for(int j=size-1;j>i;j--){
            elementDate[j-1]=elementDate[j];
            System.out.println(elementDate[j-1].toString());
        }
        elementDate[size-1]=null;
        size--;
        return 1;
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }

    @Override
    public boolean remove(Object e) {
        for(int i=0;i<elementDate.length;i++){
            if (e==elementDate[i]){
                remove(i);
                return true;
            }
        }
            return false;
    }

    @Override
    public Object replace(int i, Object e) {
        return null;
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append("[");
        for (int i=0;i<size;i++){
            if(i!=size-1) {
                stringBuilder.append(elementDate[i] + ",");
            }else{
                stringBuilder.append(elementDate[i]);
            }
        }
        stringBuilder.append("]");

        return stringBuilder.toString();
    }
}
